package com.example.sportgoods.controller;

import com.example.sportgoods.domain.Order;
import com.example.sportgoods.Service.OrderService;
import com.example.sportgoods.util.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/order")
@Api(tags = "订单管理(0528王雪伊)")
public class OrderController {

    @Autowired
    private OrderService orderService;

    // 获取所有订单列表的接口
    @ApiOperation(value = "获取所有订单列表", notes = "从数据库中查询并返回所有订单信息列表")
    @GetMapping("/list")
    public AjaxResult list() {
        List<Order> allOrders = orderService.selectAllOrders();
        return AjaxResult.success(allOrders);
    }

    // 根据订单ID获取订单详情的接口
    @ApiOperation(value = "根据订单ID获取订单详情", notes = "依据传入的订单ID，从数据库中查询并返回对应的订单详细信息")
    @ApiImplicitParam(name = "id", value = "订单ID", required = true, dataType = "Long", paramType = "path", dataTypeClass = Long.class)
    @GetMapping("/{id}")
    public AjaxResult getOrderById(@PathVariable Long id) {
        Order order = orderService.selectOrderById(id);
        return AjaxResult.success(order);
    }
    // 修改订单信息的接口
    @ApiOperation("修改订单信息")
    @PutMapping
    public AjaxResult edit(@RequestBody Order order) {
        return AjaxResult.success(orderService.updateOrder(order));
    }

    // 新增订单的接口
    @ApiOperation("新增订单")
    @PostMapping
    public AjaxResult add(@RequestBody Order order) {
        orderService.addOrder(order);
        return AjaxResult.success("订单新增成功");
    }

    // 根据订单id删除订单的接口
    @ApiOperation("根据id删除订单")
    @ApiImplicitParam(name = "id", value = "订单id", required = true, dataType = "Long", paramType = "path", dataTypeClass = Long.class)
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        orderService.deleteOrderById(id);
        return AjaxResult.success("订单删除成功");
    }
}